from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render

from django.shortcuts import render
from django.urls import reverse_lazy, reverse
from django.views.generic import FormView, CreateView
from database.models import UserInfo, Administrator, Seller, NormalUser
from database.forms import *

# Create your views here.

def Welcome(request):
    return render(request, 'account/welcome.html', )

class Login(FormView):
    model = UserInfo
    form_class = UserLoginForm
    template_name = "account/login.html"
    success_url = reverse_lazy("delivery_account:welcome")

def verify(request):
    if request.method == 'POST':
        account = request.POST['account']
        password = request.POST['password']
        corr_account = UserInfo.objects.filter(account=account).first()
        if corr_account and account == corr_account.account and password == corr_account.password:
            if corr_account.userType == 0:
                realID = Administrator.objects.filter(account=account).first()
                id = realID.id
                return HttpResponseRedirect(reverse('delivery_database:canteen_list', args=[int(id)]))
            elif corr_account.userType == 1:
                realID = Seller.objects.filter(account=account).first()
                id = realID.id
                return HttpResponseRedirect(reverse('delivery_database:seller_menu', args=[int(id)]))
            else:
                realID = NormalUser.objects.filter(account=account).first()
                id = realID.id
                return HttpResponseRedirect(reverse('delivery_database:normal_user_menu', args=[int(id)]))
        else:
            return HttpResponse('登录失败')

    return HttpResponse('状态错误')


class Register(CreateView):
    model = UserInfo
    form_class = UserRegisterForm
    template_name = "account/register.html"
    # success_url = reverse("delivery_account:detail_verify", args=)

    # 提交以后由该方法处理数据
    def post(self, request, *args, **kwargs):
        # 验证数据
        data = request.POST
        form = UserRegisterForm(data=data)
        if form.is_valid():
            # 保存数据
            user = UserInfo(
                account=form.cleaned_data['account'],
                password=form.cleaned_data['password'],
                userName=form.cleaned_data['userName'],
                userType=form.cleaned_data['userType'],
                telNum=form.cleaned_data['telNum'],
            )
            account = form.cleaned_data['account']
            userType = form.cleaned_data['userType']
            user.save()
        else:
            raise Exception

        # 跳转
        return HttpResponseRedirect(reverse('delivery_account:detail_verify', args=[account, userType])) # 反向解析url并返回


def Detailverify(request, account, type):
    # account = request.GET.get('account')
    # userType = request.GET.get('userType')
    userType = type
    if userType == 0:
        return HttpResponseRedirect(reverse('delivery_account:register_administrator'))
    elif userType == 1:
        return HttpResponseRedirect(reverse('delivery_account:register_seller'))
    else:
        return HttpResponseRedirect(reverse('delivery_account:register_normal_user'))

class RegisterAministrator(CreateView):
    model = Administrator
    form_class = AdministratorForm
    template_name = "account/register_admin.html"
    success_url = reverse_lazy("delivery_account:login")

class RegisterSeller(CreateView):
    model = Seller
    form_class = SellerForm
    template_name = "account/register_seller.html"
    success_url = reverse_lazy("delivery_account:login")

class RegisterNormalUser(CreateView):
    model = NormalUser
    form_class = NormalUserForm
    template_name = "account/register_normal.html"
    success_url = reverse_lazy("delivery_account:login")